﻿using ZYCommon;
using System;
using System.Collections;
using System.Text;
using System.Windows.Forms;
using Bifrost;
using System.Data;
using System.Text.RegularExpressions;

namespace TextEditor
{


    public class A_ConvertTextToSelect : A_Action
    {
        private int ItemCount = 0;
        public KB_List myOwnerKBList = null;

        public override string ActionName()
        {
            return "converttexttoselect";
        }

        public override bool Execute()
        {
            this.ItemCount = 0;
            if ((this.myOwnerKBList != null) && !this.myOwnerKBList.HasChildren())
            {
                string selectedText = base.myOwnerDocument.Content.GetSelectedText();
                if (selectedText != null)
                {
                    foreach (string str2 in selectedText.Split("、,;，；".ToCharArray()))
                    {
                        string str3 = str2.Trim();
                        if (str2.Length > 0)
                        {
                            KB_Item item = this.myOwnerKBList.NewItem(str2);
                            if (item == null)
                            {
                                return false;
                            }
                            item.ItemValue = str2;
                            item.ItemStyle = 0;
                            this.ItemCount++;
                        }
                    }
                    if (this.ItemCount > 0)
                    {
                        ZYTextSelect mySelect = new ZYTextSelect();
                        mySelect.OwnerKBList = this.myOwnerKBList;
                        mySelect.Text = "[" + this.myOwnerKBList.Name + "]";
                        base.myOwnerDocument._InsertSelect(mySelect);
                        return true;
                    }
                }
            }
            return false;
        }

        private string[] GetItems()
        {
            string selectedText = base.myOwnerDocument.Content.GetSelectedText();
            if (selectedText != null)
            {
                string[] strArray = selectedText.Split("、,;，；".ToCharArray());
                ArrayList list = new ArrayList();
                foreach (string str2 in strArray)
                {
                    if (str2.Trim().Length > 0)
                    {
                        list.Add(str2.Trim());
                    }
                }
                if (list.Count > 0)
                {
                    return (string[])list.ToArray(typeof(string));
                }
            }
            return null;
        }

        public override bool isEnable()
        {
            return (base.myOwnerDocument.CanModify());
        }

        /// <summary>
        /// 自动生成EvalueCode
        /// </summary>
        /// <returns></returns>
        private string GenanlEvalueCode()
        {
            DataSet ds = App.GetDataSet("select max(objectid) from ET_DOCUMENT");
            if (ds != null)
            {
                return Convert.ToString(Convert.ToInt32(ds.Tables[0].Rows[0][0]) + 1);
            }
            else
            {
                return "1";
            }
        }


        /// <summary>
        /// 把文本转换为知识列表
        /// </summary>
        /// <returns></returns>
        public override bool UIExecute()
        {
            string strData = dlgInputBox.InputBox("行间距设置  范围0~20之间", "输入数字", this.OwnerDocument.Info.LineSpacing.ToString());
            if (strData == null)
            {
                return true;
            }
            if (Regex.Match(strData.Trim(), @"^[0-9]+$").Success)
            {
                int number = Convert.ToInt32(strData);
                if (number > 20)
                {
                    App.Msg("行间距大于20px");
                }
                else
                {
                    this.OwnerDocument.Info.LineSpacing = number;
                    this.OwnerDocument.Info.ParagraphSpacing = number;
                    this.OwnerDocument.RefreshSize();
                    this.OwnerDocument.ContentChanged();
                }
            }
            else
            {

            }
            return true;           
        }
    }
}

